HEX
Server: Apache/2.4.68 (Debian)
System: Linux as-cs-widget-demo-us-central1 6.1.0-44-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.164-1 (2026-03-09) x86_64
User: root (0)
PHP: 8.2.32
Disabled: NONE
Upload Files
File: /var/www/kevin-demo/wp-content/plugins/wpforms-sendinblue/src/Provider/Actions/TrackEventAction.php
<?php

namespace WPFormsSendinblue\Provider\Actions;

use WPFormsSendinblue\Api\Exceptions\ResponseException;
use WPFormsSendinblue\Provider\CustomFields\FieldMapper;
use WPFormsSendinblue\Api\Exceptions\RequiredArgumentMissingException;

/**
 * Class TrackEventAction.
 *
 * @since 1.0.0
 */
class TrackEventAction extends Action {

	/**
	 * Run action.
	 *
	 * @since 1.0.0
	 *
	 * @throws ResponseException Invalid response.
	 * @throws RequiredArgumentMissingException Required argument is missing.
	 */
	public function run() {

		$email = sanitize_email( $this->field_mapper->get_field_value_by_id( $this->field_mapper->get_required_field_value( 'email', 'field_id' ) ) );

		if ( ! $email ) {
			throw new RequiredArgumentMissingException( 'email' );
		}

		$event_name = sanitize_text_field( $this->field_mapper->get_required_field_value( 'event_name', 'value' ) );
		$response   = $this->connection->track_event(
			$email,
			$event_name,
			$this->map_properties(),
			$this->get_form_data()
		);

		if ( $response->has_errors() ) {
			throw new ResponseException( esc_html( $response->get_response_message() ) );
		}
	}

	/**
	 * Get list of additional form information.
	 *
	 * @since 1.1.0
	 *
	 * @return array
	 */
	private function get_form_data() {

		$form_data = [
			'connection_name' => $this->connection_data['name'],
			'submitted_at'    => $this->field_mapper->get_form_data_value( 'created' ),
			'form_id'         => $this->field_mapper->get_form_data_value( 'id' ),
			'form_title'      => $this->field_mapper->get_form_data_value( 'settings', 'form_title' ),
		];

		if ( ! empty( $this->field_mapper->get_entry_id() ) ) {
			$form_data['entry_id'] = $this->field_mapper->get_entry_id();
		}

		/**
		 * Modify the form data for the track event API.
		 *
		 * @since 1.1.0
		 *
		 * @param array       $form_data    The additional form data for event tracking API.
		 * @param FieldMapper $field_mapper The object allows converting form data to a special Sendinblue format.
		 */
		return (array) apply_filters(
			'wpforms_sendinblue_provider_actions_trackeventaction_get_form_data',
			$form_data,
			$this->field_mapper
		);
	}

	/**
	 * Map Sendinblue properties and form fields.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	private function map_properties() {

		$properties = [];

		if ( empty( $this->connection_data['fields_meta'] ) ) {
			return $properties;
		}

		foreach ( $this->connection_data['fields_meta'] as $key => $field ) {
			if ( ! isset( $field['name'] ) ) {
				continue;
			}

			$field_id = $this->field_mapper->get_custom_field_value( $field['name'] );

			if ( wpforms_is_empty_string( $field_id ) ) {
				continue;
			}

			$value = $this->field_mapper->get_field_value_by_id( $field_id );

			if ( wpforms_is_empty_string( $value ) ) {
				continue;
			}

			$properties[ $field['name'] ] = $value;
		}

		return $properties;
	}
}